const fs = require('fs')
const path = require('path')
const db = require('../db')

// 获取会话列表
exports.getChatList = (req, res) => {
  const { wx_id } = req.query

  const getList = 'select * from easy_chat.conversations where user1_id = ?'
  const selectNickname = 'select * from easy_chat.users where wx_id = ?'
  db.query(getList, wx_id, async (err, results) => {
    if (err) return res.errHandle(err)
    
    // console.log(results)

    const chatList = []
    for (const conversation of results) {
      // console.log(conversation)
      // 获取 nickname
      try {
        const nicknameRes = await new Promise((resolve, reject) => {
          db.query(selectNickname, conversation.user2_id, (err, results) => {
            if (err) reject(err)
            else resolve(results)
          })
        })

        // console.log(nicknameRes)

        nicknameRes.forEach(nick => {
          chatList.push({
            conversation_id: conversation.conversation_id,
            sender_id: conversation.user1_id,
            receiver_id: conversation.user2_id,
            nickname: nick.nickname,
            avatar_url: nick.avatar_url,
            last_message_id: conversation.last_message_id,
            last_message_content: conversation.last_message_content,
            last_message_time: conversation.last_message_time,
            unread_count: conversation.unread_count
          })
        })
      } catch (err) {
        return res.errHandle(err)
      }
    }

    res.send({
      code: 200,
      message: 'success',
      data: {
        chatList
      }
    })
  })
}

// 设置新会话
exports.setChat = (req, res) => {
  const { sender_id, receiver_id } = req.query

  const setNewchat = 'insert into easy_chat.conversations set ?'
  db.query(setNewchat, { user1_id: sender_id, user2_id: receiver_id }, (err) => {
    if (err) return res.errHandle(err)
    
    // console.log(results)
    db.query(setNewchat, { user1_id: receiver_id, user2_id: sender_id }, (err) => {
      if (err) return res.errHandle(err)

      res.send({
        code: 200,
        message: 'success',
        data: {
          message: '开启新会话'
        }
      })
    })
  })
}

// 获取聊天记录
exports.getContent = (req, res) => {
  const { sender_id, receiver_id } = req.query

  const getContentItem = 'select * from easy_chat.messages where (sender_id = ? and receiver_id = ?) or (receiver_id = ? and sender_id = ?) order by created_at desc'
  db.query(getContentItem, [ sender_id, receiver_id, sender_id, receiver_id ], (err, results) => {
    if (err) return res.errHandle(err)

    // console.log(results)
    const contents = []
    results.forEach(ele => {
      if (ele.message_type === 1) {
        contents.push({
          message_id: ele.message_id,
          sender_id: ele.sender_id,
          receiver_id: ele.receiver_id,
          message_type: ele.message_type,
          content: ele.content,
          is_read: ele.is_read,
          created_at: ele.created_at
        })
      } else if (ele.message_type === 2 || ele.message_type === 4) {
        contents.push({
          message_id: ele.message_id,
          sender_id: ele.sender_id,
          receiver_id: ele.receiver_id,
          message_type: ele.message_type,
          content: ele.message_id,
          is_read: ele.is_read,
          created_at: ele.created_at
        })
      }
    })

    res.send({
      code: 200,
      message: 'success',
      data: {
        contents
      }
    })
  })
}
